package com.test.install;

import javax.annotation.Resource;

import org.apache.commons.codec.digest.DigestUtils;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;

import com.test.domain.Priviledge;
import com.test.domain.User;
/**
 * 用于初始化系统数据：
 * <ul>
 * 	<li>1.初始化系统管理员数据</li>
 * 	<li>2.初始化权限数据</li>
 * </ul>
 * @author SingleHe
 *
 */
@Controller
public class Installer {
	@Resource
	private SessionFactory sessionFactory;
	//要开启事务
	@Transactional
	public void Install(){
		Session session = sessionFactory.getCurrentSession();
		//系统管理员数据
		User user = new User();
		user.setLoginName("admin");
		user.setName("系统管理员");
		user.setPassword(DigestUtils.md5Hex("123456"));
		session.save(user);
		//权限数据
		Priviledge menu, menu1,menu2,menu3,menu4,menu5;
		/**
		 * |-系统管理
		 * 	  |-岗位管理
		 * 		|-岗位列表
		 * 		|-新增岗位
		 * 		|-删除岗位
		 * 		|-修改岗位
		 * 	  |-部门管理
		 * 		|-部门列表
		 * 		|-新增部门
		 * 		|-修改部门
		 * 		|-删除部门
		 *    |-用户管理
		 *      |-。。。
		 * |-网上交流
		 * |-审批流转
		 */
		menu = new Priviledge("系统管理", null, null);
		menu1 = new Priviledge("岗位管理", "/role_list", menu);
		menu2 = new Priviledge("部门管理", "/department_list", menu);
		menu3 = new Priviledge("用户管理", "/user_list", menu);
		session.save(menu);
		session.save(menu1);
		session.save(menu2);
		session.save(menu3);
		//岗位管理
		session.save(new Priviledge("岗位列表", "/role_list", menu1));
		session.save(new Priviledge("新增岗位", "/role_add", menu1));
		session.save(new Priviledge("删除岗位", "/role_delete", menu1));
		session.save(new Priviledge("修改岗位", "/role_edit", menu1));
		//部门管理
		session.save(new Priviledge("部门列表", "/department_list", menu2));
		session.save(new Priviledge("新增部门", "/department_add", menu2));
		session.save(new Priviledge("删除部门", "/department_delete", menu2));
		session.save(new Priviledge("修改部门", "/department_edit", menu2));
		//用户管理
		session.save(new Priviledge("用户列表", "/user_list", menu3));
		session.save(new Priviledge("新增用户", "/user_add", menu3));
		session.save(new Priviledge("删除用户", "/user_delete", menu3));
		session.save(new Priviledge("修改用户", "/user_edit", menu3));
		session.save(new Priviledge("初始化用户密码", "/user_initPassword", menu3));
		//网上交流模块
		menu = new Priviledge("网上交流", null, null);
		menu1 = new Priviledge("论坛管理", "/forumManage_list", menu);
		menu2 = new Priviledge("论坛","/forum_list",menu);
		session.save(menu);
		session.save(menu1);
		session.save(menu2);
		
		//审批流转模块
		menu = new Priviledge("审批流转",null, null);
		menu1 = new Priviledge("审批流程管理", "/processDefinition_list", menu);
		menu2 = new Priviledge("申请模板管理", "/template_list", menu);
		menu3 = new Priviledge("起草申请", "/flow_templateList", menu);
		menu4 = new Priviledge("待我审批", "/flow_myTaskList", menu);
		menu5 = new Priviledge("我的申请查询", "/flow_myApplicationList", menu);
		session.save(menu);
		session.save(menu1);
		session.save(menu2);
		session.save(menu3);
		session.save(menu4);
		session.save(menu5);
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("开始初始化系统数据...");
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		Installer installer = (Installer) ac.getBean("installer");
		installer.Install();
		System.out.println("系统数据初始化完毕...!");
	}
}
